package cn.haoma.base.utils;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class L {
    public static void e(String str) {
        e("", str);
    }

    public static void e(String tag, String str) {

        Log.e(tag, str);
    }

    public static void e(Exception e) {
        e("", e);
    }

    public static void e(String tag, Exception e) {

        for (StackTraceElement stackTraceElement : e.getStackTrace()) {
            Log.e(tag, stackTraceElement.toString());
        }
    }

    public static void e(Error e) {
        e("", e);
    }

    public static void e(String tag, Error e) {

        for (StackTraceElement stackTraceElement : e.getStackTrace()) {
            Log.e(tag, stackTraceElement.toString());
        }
    }

    public static void i(String str) {
        i("", str);
    }

    public static void i(String tag, String str) {

        Log.i(tag, str);
    }

    public static void saveToLog(String str) {
        File dir = new File(Environment.getExternalStorageDirectory(), "caocao");
        saveToLog(dir, str);
    }

    public static void saveToLog(File dir, String str) {
        //        if(dir.isFile()){
        //            throw new IllegalArgumentException("notice the difference between saveToLog(File dir, String str) and saveToLog(String str, File logFile)");
        //        }
        try {
            dir.mkdirs();
            File logFile = new File(dir, new SimpleDateFormat("MM-dd-").format(Calendar.getInstance().getTime()) + "log.txt");
            FileOutputStream fos = new FileOutputStream(logFile, true);
            PrintWriter printWriter = new PrintWriter(fos);
            printWriter.print('\n');
            printWriter.print(new SimpleDateFormat("hh:mm:ss").format(Calendar.getInstance().getTime()));
            printWriter.print('\n');
            printWriter.print(str);
            printWriter.flush();
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void saveToLog(String str, File logFile) {
        //        if(logFile.isDirectory()){
        //            throw new IllegalArgumentException("notice the difference between saveToLog(File dir, String str) and saveToLog(String str, File logFile)");
        //        }
        try {
            File dir = logFile.getParentFile();
            dir.mkdirs();
            FileOutputStream fos = new FileOutputStream(logFile, true);
            PrintWriter printWriter = new PrintWriter(fos);
            printWriter.print(str);
            printWriter.print('\n');
            printWriter.flush();
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}
